import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.opengauss.copy.CopyManager;
import org.opengauss.core.BaseConnection;

/**
 * @作者： @wan005
 * @时间： 2023/3/3
 **/

public class jdbc_Certification_Case0007 {

    static public String driver = "org.opengauss.Driver";
    public static Properties getConfigFromFile(String filePath) {
        Properties props = new Properties();
        try {
            BufferedInputStream config = new BufferedInputStream(new FileInputStream(filePath));
            props.load(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return props;
    }

    public static String genURLFromPro(Properties props) {
        String hostname = props.getProperty("hostname");
        String[] hostnames = hostname.split(",");
        String port = props.getProperty("port");
        String[] ports = port.split(",");
        String dbname = props.getProperty("dbname");
        return genURLFromHostsPortsDBname(hostnames, ports, dbname);
    }

    public static String genURLFromHostsPortsDBname(String[] hostnames, String[] ports, String dbname) {
        String sourceURL;
        if (hostnames.length == 1) {
            sourceURL = "jdbc:opengauss://" + hostnames[0] + ":" + ports[0] + "/" + dbname;
        } else {
            ArrayList<String> ip_port_pare = new ArrayList<>();
            if (ports.length == 1) {
                for (String ip : hostnames) {
                    ip_port_pare.add(ip + ":" + ports[0]);
                }
            } else {
                for (int i = 0; i < hostnames.length; i++) {
                    ip_port_pare.add(hostnames[i] + ":" + ports[i]);
                }
            }
            sourceURL = "jdbc:opengauss://" + String.join(",", ip_port_pare) + "/" + dbname;
        }
        return sourceURL;
    }

    public static Connection GetConnection(Properties props) {
        Connection connR;
        String sourceURL = genURLFromPro(props);
        System.out.println(sourceURL);
        try {
            Class.forName(driver).newInstance();
            connR = DriverManager.getConnection(sourceURL, props);
        } catch (Exception var10) {
            var10.printStackTrace();
            return null;
        }
        return connR;
    }

    public static void main(String[] args) {
        Properties pros = new Properties();
        Properties pros_conf=null;
        String jdbc_config_file = null;
        String table = "t_certify_0007";
        for (int i = 0; i < args.length; i++) {
            switch (args[i]) {
                case "--config-file":
                    jdbc_config_file = args[i + 1];
                    pros_conf = getConfigFromFile(jdbc_config_file);
                    break;
                case "-F":
                    jdbc_config_file = args[i + 1];
                    pros_conf = getConfigFromFile(jdbc_config_file);
                    break;
                default:
                    break;
            }
        }
        if (pros_conf != null){
            pros_conf.putAll(pros);
        }
        Connection conn = GetConnection(pros_conf);
        System.out.println(conn);
        if(conn == null){
            System.out.println("连接失败");
            return;
        }else{
            try{
                System.out.println("1.-----查看审计参数-----");
                String[] sql7_01 = {"show audit_enabled;",
                                 "show audit_dml_state;"};
                Statement stmt=conn.createStatement();
                for (int i = 0; i < sql7_01.length; i++) {
                    boolean hasResultSet = stmt.execute(sql7_01[i]);
                    if (hasResultSet) {
                        ResultSet rs = stmt.getResultSet();
                        while (rs.next()){
                            System.out.println("7_01.第" + i + "行结果:" + 
                                rs.getString(1));
                        }
                    } else {
                        int count = stmt.getUpdateCount();
                        System.out.println("7_01.第" + i + "行更新:" + count);
                    }
                }
                stmt.close();

                System.out.println("2.-----执行DML操作，并查看审计日志-----");
                String[] sql7_02 = {"select sysdate;",
                                    "select pg_sleep(2);",
                                    "create table " + table + "(a int);",
                                    "insert into " + table + " values(1)",
                                    "select pg_sleep(2);",
                                    "select sysdate;"};
                String start_time = "";
                String end_time = "";
                Statement stmt2=conn.createStatement();
                for (int i = 0; i < sql7_02.length; i++) {
                    boolean hasResultSet = stmt2.execute(sql7_02[i]);
                    if (hasResultSet) {
                        ResultSet rs = stmt2.getResultSet();
                        while (rs.next()){
                            System.out.println("7_02.第" + i + "行结果:" + 
                                rs.getString(1));
                            if (i == 0) {
                                start_time = rs.getString(1);
                            } else if (i == 5) {
                                end_time= rs.getString(1);
                            }
                        }
                    } else {
                        int count = stmt2.getUpdateCount();
                        System.out.println("7_02.第" + i + "行更新:" + count);
                    }
                }
                stmt2.close();
                if (start_time == "" || end_time == "") {
                    System.out.println("ERROR:Do not contain time info");;
                }

                System.out.println("2.-----执行DML操作，并查看审计日志-----");
                String sql7_03 = "select * from pg_query_audit('" +
                    start_time + "','" + end_time + "') order by time desc;";
                System.out.println(sql7_03);
                Statement stmt3=conn.createStatement();
                boolean hasResultSet = stmt3.execute(sql7_03);
                if (hasResultSet) {
                    ResultSet rs = stmt3.getResultSet();
                    while (rs.next()){
                        System.out.println("7_03.结果:" + 
                            rs.getString(9));
                    }
                } else {
                    int count = stmt3.getUpdateCount();
                    System.out.println("7_03.更新:" + count);
                }
                stmt3.close();

                String sqldel = "drop table if exists " + table + ";";
                Statement stmtdel=conn.createStatement();
                boolean hasResultSetdrop = stmtdel.execute(sqldel);
                if (hasResultSetdrop) {
                    ResultSet rs = stmtdel.getResultSet();
                    while (rs.next()){
                        System.out.println("7_drop.结果:" + rs.getString(1));
                    }
                } else {
                    int count = stmtdel.getUpdateCount();
                    System.out.println("7_drop.更新行:" + count);
                }
                stmtdel.close();
            }catch (SQLException s){
                s.printStackTrace();
            }
        }
    }
}